home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 March / PCWMAR06.iso / Software / Freeware / Thingamablog 1.0.5 / thinga-setup-1.0.5.exe / tb_legacy.jar / RSSWriter.java < prev   
Encoding:
Java Source  |  2004-03-20  |  7.7 KB  |  254 lines

  1. /*
  2.  * Copyright (C) 2003  Bob Tantlinger
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  *
  18.  */
  19.  
  20. import java.io.*;
  21. import java.util.*;
  22. import java.text.SimpleDateFormat;
  23.  
  24. public class RSSWriter
  25. {
  26.     final String RSS_VERSION_INFO =
  27.         "<?xml version=\"1.0\" ?>\n"
  28.             + "<rdf:RDF xmlns=\"http://purl.org/rss/1.0/\" "
  29.             + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
  30.             + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xml:lang=\"en\">";
  31.  
  32.     BlogVariables blogVars;
  33.     File rssFile;
  34.     PrintWriter pw;
  35.     String url = "";
  36.     SimpleDateFormat sdf;
  37.     int wordLen = 20;
  38.  
  39.     boolean isWriteFullEntry = true;
  40.  
  41.     public RSSWriter(File f, BlogVariables b, boolean writeFullEntries) throws IOException
  42.     {
  43.         sdf = new SimpleDateFormat();
  44.         sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
  45.         sdf.applyPattern("EEE, dd MMM yyyy HH:mm:ss z");        
  46.         isWriteFullEntry = writeFullEntries;        
  47.         pw = new PrintWriter(new FileWriter(f));
  48.         rssFile = f;
  49.         blogVars = b;
  50.     }
  51.  
  52.     public void writeEntry(BlogEntry be, String arcPage) throws IOException
  53.     {
  54.         String title = replaceHTMLTags(be.getTitle());
  55.         String entry;
  56.         if (isWriteFullEntry)
  57.             entry = replaceHTMLTags(be.getEntryText());
  58.         else
  59.             entry =
  60.                 removeHTMLTags(firstWords(be.getEntryText(), wordLen)) + "...";
  61.  
  62.         String arcUrl = blogVars.getArchivesUrl();
  63.         if (!arcUrl.endsWith("/"))
  64.             arcUrl += "/";
  65.         arcUrl += arcPage + "#" + be.getID();
  66.  
  67.         pw.println("<item rdf:about=\"" + arcUrl + "\">");
  68.         pw.println("<title>" + title + "</title>");
  69.         pw.println("<link>" + arcUrl + "</link>");
  70.         pw.println("<description>" + entry + "</description>");
  71.         pw.println("<pubDate>" + sdf.format(be.getTimestamp()) + "</pubDate>");
  72.         pw.println("</item>");
  73.     }
  74.  
  75.     private String firstWords(String str, int n)
  76.     {
  77.         StringTokenizer st = new StringTokenizer(str);
  78.         int count = 0;
  79.         String words = "";
  80.  
  81.         while (st.hasMoreTokens() && count < n)
  82.         {
  83.             words += st.nextToken() + ' ';
  84.             count++;
  85.         }
  86.  
  87.         return words;
  88.     }
  89.  
  90.     /*private String replaceHTMLTags(String s)
  91.     {
  92.         if(s.indexOf('<') < 0 && s.indexOf('>') < 0 && s.indexOf('&') < 0)
  93.             return s;
  94.         
  95.         String l = "<";
  96.         String r = ">";
  97.         
  98.         StringBuffer sb = new StringBuffer(s);
  99.         for(int i = 0; i < sb.length(); i++)
  100.         {
  101.             if(sb.charAt(i) == '&')
  102.             {                            
  103.                 sb.deleteCharAt(i);
  104.                 sb.insert(i, "&");
  105.             }
  106.             else if(sb.charAt(i) == '<')
  107.             {
  108.                 sb.deleteCharAt(i);
  109.                 sb.insert(i, l);
  110.             }            
  111.             else if(sb.charAt(i) == '>')
  112.             {
  113.                 sb.deleteCharAt(i);
  114.                 sb.insert(i, r);
  115.             }                    
  116.         }
  117.         
  118.         return sb.toString();    
  119.     } */
  120.  
  121.     public String replaceHTMLTags(String string)
  122.     {
  123.         StringBuffer sb = new StringBuffer(string.length());
  124.         // true if last char was blank
  125.         boolean lastWasBlankChar = false;
  126.         int len = string.length();
  127.         char c;
  128.  
  129.         for (int i = 0; i < len; i++)
  130.         {
  131.             c = string.charAt(i);
  132.             if (c == ' ')
  133.             {
  134.                 // blank gets extra work,
  135.                 // this solves the problem you get if you replace all
  136.                 // blanks with  , if you do that you loss 
  137.                 // word breaking
  138.                 if (lastWasBlankChar)
  139.                 {
  140.                     lastWasBlankChar = false;
  141.                     sb.append("&nbsp;");
  142.                 }
  143.                 else
  144.                 {
  145.                     lastWasBlankChar = true;
  146.                     sb.append(' ');
  147.                 }
  148.             }
  149.             else
  150.             {
  151.                 lastWasBlankChar = false;
  152.                 //
  153.                 // HTML Special Chars
  154.                 if (c == '"')
  155.                     sb.append(""");
  156.                 else if (c == '&')
  157.                     sb.append("&");
  158.                 else if (c == '<')
  159.                     sb.append("<");
  160.                 else if (c == '>')
  161.                     sb.append(">");
  162.                 //else if (c == '\n')
  163.                 // Handle Newline
  164.                 //    sb.append("<br/>");
  165.                 else
  166.                 {
  167.                     int ci = 0xffff & c;
  168.                     if (ci < 160)
  169.                         // nothing special only 7 Bit
  170.                         sb.append(c);
  171.                     else
  172.                     {
  173.                         // Not 7 Bit use the unicode system
  174.                         sb.append("&#");
  175.                         sb.append(new Integer(ci).toString());
  176.                         sb.append(';');
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.         return sb.toString();
  182.     }
  183.  
  184.     // added by John Montgomery - strips HTML tags from entries
  185.     private String removeHTMLTags(String s)
  186.     {
  187.         if (s.indexOf('<') < 0 && s.indexOf('>') < 0 && s.indexOf('&') < 0)
  188.             return s;
  189.  
  190.         StringBuffer buffer = new StringBuffer(s.length());
  191.  
  192.         int index = -1;
  193.         while ((index = s.indexOf('<')) != -1)
  194.         {
  195.             String head = s.substring(0, index);
  196.             String tail = s.substring(index);
  197.             buffer.append(head);
  198.             index = tail.indexOf('>');
  199.             if (index != -1) // if it's -1 we're partway thru a tag
  200.             {
  201.                 tail = tail.substring(index + 1);
  202.             }
  203.             else
  204.             {
  205.                 //convert broken tags or trailing '<' to < 
  206.                 //so we don't get stuck in an infinte loop                
  207.                 tail = replaceHTMLTags(tail);
  208.             }
  209.             s = tail;
  210.         }
  211.         buffer.append(s); // add what ever is left over        
  212.         return replaceHTMLTags(buffer.toString());
  213.     }
  214.  
  215.     public void writeHeader() throws IOException
  216.     {
  217.         String lastBuild = sdf.format(new Date());
  218.         String url = blogVars.getBaseUrl();
  219.         if (!url.endsWith("/"))
  220.             url += "/";
  221.  
  222.         pw.println(RSS_VERSION_INFO);
  223.         pw.println(
  224.             "<channel rdf:about=\""
  225.                 + url
  226.                 + rssFile.getName()
  227.                 + "\">\n"
  228.                 + "<title>"
  229.                 + blogVars.getTitle()
  230.                 + "</title>\n"
  231.                 + "<link>"
  232.                 + url
  233.                 + blogVars.getMainPageFileName()
  234.                 + "</link>\n"
  235.                 + "<description>"
  236.                 + blogVars.getDescription()
  237.                 + "</description>\n"
  238.                 + "<lastBuildDate>"
  239.                 + lastBuild
  240.                 + "</lastBuildDate>\n"
  241.                 + "</channel>\n");
  242.     }
  243.  
  244.     public void writeFooter() throws IOException
  245.     {
  246.         pw.println("</rdf:RDF>");
  247.     }
  248.  
  249.     public void close() throws IOException
  250.     {
  251.         pw.close();
  252.     }
  253. }
  254.